package com.bjesc_b2c.app.appraiset.utils;

import android.content.Context;
import android.text.TextUtils;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.rxretrofit_build.CarPhotoID;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import master_bean.CarForBean;
import master_bean.CarSourcePhotoBean;
import master_bean.CountryVariousCityBean;
import master_bean.DialogPureTextBean;
import master_bean.LastTransferredAddressBean;

/**
 * Created by Jason Chen on 2017/2/23.
 */

public class TimeListUilts {

    /**
     * 前30年
     *
     * @return
     */
    public static List<String> getBeforeYearList() {
        List<String> yearList = new ArrayList<>();
        Calendar calendar = Calendar.getInstance();
        int year = calendar.get(Calendar.YEAR);
        for (int i = year; i > year - 30; i--) {
            yearList.add(i + "年");
        }
        return yearList;
    }

    /**
     * 后30年
     *
     * @return
    */
    public static List<String> getAftertimeYearList() {
        List<String> yearList = new ArrayList<>();
        Calendar calendar = Calendar.getInstance();
        int year = calendar.get(Calendar.YEAR);
        for (int i = year; i < year + 30; i++) {
            yearList.add(i + "年");
        }
        return yearList;
    }

    /**
     * 前后15年
     *
     * @return
     */
    public static List<String> getAroundYearList() {
        List<String> yearList = new ArrayList<>();
        Calendar calendar = Calendar.getInstance();
        int year = calendar.get(Calendar.YEAR);
        for (int i = year - 15; i < year; i++) {
            yearList.add(i + "年");
        }

        for (int i = year; i <= year + 15; i++) {
            yearList.add(i + "年");
        }
        return yearList;
    }

    public static List<String> getMonthList() {
        List<String> monthList = new ArrayList<>();
        for (int i = 1; i <= 12; i++) {
            monthList.add(i + "月");
        }
        return monthList;
    }

    public static List<String> getBeforeDayList() {
        List<String> dayList = new ArrayList<>();
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(System.currentTimeMillis());
        int dayIndex = calendar.get(Calendar.DAY_OF_MONTH);
//        for (int i = dayIndex; i >= 1; i--) {
//            dayList.add(i + "日");
//        }
        return dayList;
    }

    public static List<String> getAftertimeDayList() {
        List<String> dayList = new ArrayList<>();
        Calendar calendar = Calendar.getInstance();
//        calendar.setTimeInMillis(System.currentTimeMillis());
        int yearIndex = calendar.get(Calendar.YEAR);
        int monthIndex = calendar.get(Calendar.MONTH)+1;
        int dayIndex = calendar.get(Calendar.DAY_OF_MONTH);
        int dayOFmonth = 0;
        if (monthIndex == 2) {
            if (((yearIndex) % 4 == 0 && (yearIndex) % 100 != 0) || (yearIndex) % 400 == 0) {
                dayOFmonth = 29;
            } else {
                dayOFmonth = 28;
            }
        } else if (monthIndex == 1 || monthIndex == 3 || monthIndex == 5 || monthIndex == 7 || monthIndex == 8 || monthIndex == 10 || monthIndex == 12) {
            dayOFmonth = 31;
        } else {
            dayOFmonth = 30;
        }
        for (int i = 1; i <= dayOFmonth; i++) {
            dayList.add(i + "日");
        }
        return dayList;
    }

    public static List<String> getLongDayList() {
        List<String> dayList = new ArrayList<>();
        for (int i = 1; i <= 31; i++) {
            dayList.add(i+"日");
        }
        return dayList;
    }
    public static List<String> getShortDayList(){
        List<String> dayList = new ArrayList<>();
        for (int i = 1; i <= 30; i++) {
            dayList.add(i+"日");
        }
        return dayList;
    }
    public static List<String> getFebruaryLongDay(){
        List<String> dayList = new ArrayList<>();
        for (int i = 1; i <= 29; i++) {
            dayList.add(i+"日");
        }
        return dayList;
    }
    public static List<String> getFebruaryShortDay(){
        List<String> dayList = new ArrayList<>();
        for (int i = 1; i <= 28; i++) {
            dayList.add(i+"日");
        }
        return dayList;
    }

    public static List<String> getAftertimeMonthList(){
        List<String> monthList = new ArrayList<>();
        Calendar calendar = Calendar.getInstance();
        int monthIndex = calendar.get(Calendar.MONTH) + 1;
        for (int i = monthIndex; i <= 12; i++) {
            monthList.add(i+"月");
        }
        return monthList;
    }
    public static List<String> getBeforeMonthList(){
        List<String> monthList = new ArrayList<>();
        Calendar calendar = Calendar.getInstance();
        int monthIndex = calendar.get(Calendar.MONTH) + 1;
        for (int i = 1; i <= monthIndex; i++) {
            monthList.add(i+"月");
        }
        return monthList;
    }

    /**
     * 标签
     *
     * @return
     */
    public static List<DialogPureTextBean> getLabelList() {
        List<DialogPureTextBean> labelList = new ArrayList<>();
        labelList.add(new DialogPureTextBean("急售", false));
        labelList.add(new DialogPureTextBean("准新车", false));
        labelList.add(new DialogPureTextBean("超值", false));
        labelList.add(new DialogPureTextBean("已初检", false));
        labelList.add(new DialogPureTextBean("独家", false));
        labelList.add(new DialogPureTextBean("寄售", false));
        return labelList;
    }

    /**
     * 燃油类型
     *
     * @return
     */
    public static List<DialogPureTextBean> getFuelList() {
        List<DialogPureTextBean> fuelList = new ArrayList<>();
        fuelList.add(new DialogPureTextBean("汽油", false));
        fuelList.add(new DialogPureTextBean("柴油", false));
        fuelList.add(new DialogPureTextBean("电动", false));
        fuelList.add(new DialogPureTextBean("油电混合", false));
        return fuelList;
    }

    public static List<DialogPureTextBean> getCarLevelList() {
        List<DialogPureTextBean> fuelList = new ArrayList<>();
        fuelList.add(new DialogPureTextBean("A", false));
        fuelList.add(new DialogPureTextBean("B", false));
        fuelList.add(new DialogPureTextBean("C", false));
        fuelList.add(new DialogPureTextBean("D", false));
        return fuelList;
    }

    public static List<DialogPureTextBean> getCostLevelList() {
        List<DialogPureTextBean> fuelList = new ArrayList<>();
        fuelList.add(new DialogPureTextBean("++", false));
        fuelList.add(new DialogPureTextBean("+", false));
        fuelList.add(new DialogPureTextBean("-", false));
        fuelList.add(new DialogPureTextBean("--", false));
        return fuelList;
    }

    /**
     * 获取城市
     *
     * @param context
     * @param type
     * @return
     */
    public static List<CountryVariousCityBean> getCountryCityList(Context context, int type) {
        String json = null;
        if (type == 1) {
            json = getFromAssets(context, "address.json");
        } else {
            json = getFromAssets(context, "yunnan_various_area.json");
        }
        if (TextUtils.isEmpty(json)) {
            return null;
        }

        return new Gson().fromJson(json, new TypeToken<List<CountryVariousCityBean>>() {
        }.getType());
    }

    /**
     * 获取最后转入地
     *
     * @param context
     * @return
     */
    public static List<LastTransferredAddressBean> getLastAddressList(Context context) {
        String json = getFromAssets(context, "last_transferred_address.json");
        return new Gson().fromJson(json, new TypeToken<List<LastTransferredAddressBean>>() {
        }.getType());
    }

    /**
     * 获取车辆来源
     *
     * @param context
     * @return
     */
    public static List<CarForBean> getCarForBean(Context context) {
        String json = getFromAssets(context, "carfor.json");
        return new Gson().fromJson(json, new TypeToken<List<CarForBean>>() {
        }.getType());
    }

    /**
     * 获取车源图片外观的集合
     *
     * @return
     */
    public static List<CarSourcePhotoBean> getSurfacePhotoList() {
        List<CarSourcePhotoBean> photoBeanList = new ArrayList<>();
        photoBeanList.add(new CarSourcePhotoBean(CarPhotoID.EngineID.leftFrontWheel, "", "左前轮内侧底盘"));
        photoBeanList.add(new CarSourcePhotoBean(CarPhotoID.EngineID.rightFrontWheel, "", "右前轮内侧底盘"));
        photoBeanList.add(new CarSourcePhotoBean(CarPhotoID.SurfaceID.leftFront45, "", "左前45°"));
        photoBeanList.add(new CarSourcePhotoBean(CarPhotoID.SurfaceID.front, "", "正前"));
        photoBeanList.add(new CarSourcePhotoBean(CarPhotoID.SurfaceID.rightFront45, "", "右前45°"));
        photoBeanList.add(new CarSourcePhotoBean(CarPhotoID.SurfaceID.leftFrontLamp, "", "车前大灯"));
//        photoBeanList.add(new CarSourcePhotoBean(CarPhotoID.SurfaceID.positiveSide, "", "正侧"));
        photoBeanList.add(new CarSourcePhotoBean(CarPhotoID.SurfaceID.tires, "", "轮胎"));
        photoBeanList.add(new CarSourcePhotoBean(CarPhotoID.SurfaceID.surfaceOther, "", "更多"));
//        photoBeanList.add(new CarSourcePhotoBean(CarPhotoID.SurfaceID.leftRear45, "", "左后45°"));
//        photoBeanList.add(new CarSourcePhotoBean(CarPhotoID.SurfaceID.rear, "", "正后"));
//        photoBeanList.add(new CarSourcePhotoBean(CarPhotoID.SurfaceID.rightRear45, "", "右后45°"));
////        photoBeanList.add(new CarSourcePhotoBean(CarPhotoID.SurfaceID.ceiling, "", "车顶"));
////        photoBeanList.add(new CarSourcePhotoBean(CarPhotoID.SurfaceID.rightFrontLamp, "", "右前大灯"));
////        photoBeanList.add(new CarSourcePhotoBean(CarPhotoID.SurfaceID.rightRearLamp, "", "右后大灯"));
//        photoBeanList.add(new CarSourcePhotoBean(CarPhotoID.SurfaceID.leftRearLamp, "", "车后大灯"));
//        photoBeanList.add(new CarSourcePhotoBean(CarPhotoID.SurfaceID.surfaceOther, "", "更多"));
        return photoBeanList;
    }
    public static List<CarSourcePhotoBean> getEnginePhotoList() {
        List<CarSourcePhotoBean> photoBeanList = new ArrayList<>();
        photoBeanList.add(new CarSourcePhotoBean(CarPhotoID.SurfaceID.leftRear45, "", "左后45°"));
        photoBeanList.add(new CarSourcePhotoBean(CarPhotoID.SurfaceID.rightRear45, "", "右后45°"));
        photoBeanList.add(new CarSourcePhotoBean(CarPhotoID.SurfaceID.leftRearLamp, "", "车后大灯"));
        photoBeanList.add(new CarSourcePhotoBean(CarPhotoID.EngineID.leftRearWheel, "", "左后轮内侧底盘"));
        photoBeanList.add(new CarSourcePhotoBean(CarPhotoID.EngineID.rightRearWheel, "", "右后轮内侧底盘"));
        photoBeanList.add(new CarSourcePhotoBean(CarPhotoID.EngineID.boot, "", "后备箱"));
        photoBeanList.add(new CarSourcePhotoBean(CarPhotoID.EngineID.spare, "", "备胎"));
        photoBeanList.add(new CarSourcePhotoBean(CarPhotoID.EngineID.engineOther, "", "更多"));
//        photoBeanList.add(new CarSourcePhotoBean(CarPhotoID.EngineID.engine, "", "发动机机舱"));
////        photoBeanList.add(new CarSourcePhotoBean(CarPhotoID.EngineID.leftEngine, "", "发动机机舱右侧"));
////        photoBeanList.add(new CarSourcePhotoBean(CarPhotoID.EngineID.rightEngine, "", "发动机机舱左侧"));
//        photoBeanList.add(new CarSourcePhotoBean(CarPhotoID.EngineID.leftFrontWheel, "", "左前轮内侧底盘"));
//        photoBeanList.add(new CarSourcePhotoBean(CarPhotoID.EngineID.rightFrontWheel, "", "右前轮内侧底盘"));
//        photoBeanList.add(new CarSourcePhotoBean(CarPhotoID.EngineID.leftRearWheel, "", "左后轮内侧底盘"));
//        photoBeanList.add(new CarSourcePhotoBean(CarPhotoID.EngineID.rightRearWheel, "", "右后轮内侧底盘"));
////        photoBeanList.add(new CarSourcePhotoBean(CarPhotoID.EngineID.leftDoor, "", "左侧门槛"));
////        photoBeanList.add(new CarSourcePhotoBean(CarPhotoID.EngineID.rightDoor, "", "右侧门槛"));
//        photoBeanList.add(new CarSourcePhotoBean(CarPhotoID.EngineID.boot, "", "后备箱"));
//        photoBeanList.add(new CarSourcePhotoBean(CarPhotoID.EngineID.spare, "", "备胎"));
//        photoBeanList.add(new CarSourcePhotoBean(CarPhotoID.EngineID.engineOther, "", "更多"));
        return photoBeanList;
    }
    /**
     * 获取车源图片内饰的集合
     *
     * @return
     */
    public static List<CarSourcePhotoBean> getOrnamentsPhotoList() {
        List<CarSourcePhotoBean> photoBeanList = new ArrayList<>();
        photoBeanList.add(new CarSourcePhotoBean(CarPhotoID.OrnamentsID.leftFrontdoor, "", "左前门"));
        photoBeanList.add(new CarSourcePhotoBean(CarPhotoID.OrnamentsID.frontRow, "", "内饰前排"));
        photoBeanList.add(new CarSourcePhotoBean(CarPhotoID.OrnamentsID.seatBelt, "", "安全带根部"));
        photoBeanList.add(new CarSourcePhotoBean(CarPhotoID.OrnamentsID.accelerator, "", "油门踏板"));
        photoBeanList.add(new CarSourcePhotoBean(CarPhotoID.OrnamentsID.steeringWheel, "", "方向盘"));
        photoBeanList.add(new CarSourcePhotoBean(CarPhotoID.OrnamentsID.steering, "", "仪表盘"));
        photoBeanList.add(new CarSourcePhotoBean(CarPhotoID.OrnamentsID.shiftLever, "", "变速杆"));
        photoBeanList.add(new CarSourcePhotoBean(CarPhotoID.OrnamentsID.leftAreadoor, "", "左后门"));
        photoBeanList.add(new CarSourcePhotoBean(CarPhotoID.OrnamentsID.rearRow, "", "内饰后排"));
        photoBeanList.add(new CarSourcePhotoBean(CarPhotoID.OrnamentsID.control, "", "内饰中控"));
        photoBeanList.add(new CarSourcePhotoBean(CarPhotoID.OrnamentsID.ceiling, "", "车内顶棚"));
//      photoBeanList.add(new CarSourcePhotoBean(CarPhotoID.OrnamentsID.seal, "", "密封胶条"));
        photoBeanList.add(new CarSourcePhotoBean(CarPhotoID.OrnamentsID.ornamentsOther, "", "更多"));
        return photoBeanList;
    }

    /**
     * 获取车源图片手续的集合
     *
     * @return
     */
    public static List<CarSourcePhotoBean> getProceduresPhotoList() {
        List<CarSourcePhotoBean> photoBeanList = new ArrayList<>();
        photoBeanList.add(new CarSourcePhotoBean(CarPhotoID.ProceduresID.outFactory, "", "出厂牌"));
//        photoBeanList.add(new CarSourcePhotoBean(CarPhotoID.ProceduresID.positiveLnsuranceCard, "", "保险卡正面"));
//        photoBeanList.add(new CarSourcePhotoBean(CarPhotoID.ProceduresID.negativeLnsuranceCard, "", "保险卡反面"));
//        photoBeanList.add(new CarSourcePhotoBean(CarPhotoID.ProceduresID.taxProof, "", "完税说明"));
        photoBeanList.add(new CarSourcePhotoBean(CarPhotoID.ProceduresID.licenseOne, "", "行车证"));
//        photoBeanList.add(new CarSourcePhotoBean(CarPhotoID.ProceduresID.licenseTwo, "", "行车证2"));
//        photoBeanList.add(new CarSourcePhotoBean(CarPhotoID.ProceduresID.licenseThree, "", "行车证3"));
        photoBeanList.add(new CarSourcePhotoBean(CarPhotoID.ProceduresID.proceduresOther, "", "更多"));
        return photoBeanList;
    }


    public static String getObjectKey() {
        long time = System.currentTimeMillis() / 1000;
        int random = (int) (Math.random() * 99999);
        return time + "_" + random;
    }

    public static String subUrl(String url) {
        if (url.contains("?")) {
            int index = url.indexOf("?");
            url = url.substring(0, index);
            int urlIndex = url.lastIndexOf("/") + 1;
            return url.substring(urlIndex, url.length());
        } else {
            int urlIndex = url.lastIndexOf("/") + 1;
            return url.substring(urlIndex, url.length());
        }
    }

    /**
     * 从本地读取地址json
     *
     * @return
     */
    public static String getFromAssets(final Context context, final String jsonName) {
        BufferedReader bufReader = null;
        StringBuilder sb = new StringBuilder();
        try {
            InputStreamReader inputReader = new InputStreamReader(
                    context.getResources().getAssets().open(jsonName));
            bufReader = new BufferedReader(inputReader);
            String line = "";
            while ((line = bufReader.readLine()) != null) {
                sb.append(line);
            }
            if (TextUtils.isEmpty(sb.toString())) {
                return "";
            }
            return sb.toString();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (bufReader != null) {
                try {
                    bufReader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return "";
    }
}
